//
//  USConnectLib.m
//  viewtest
//
//  Created by xy on 10-2-20.
//  Copyright 2010 HANDPAY. All rights reserved.
//

#import "HttpConnect.h"
#import "NSNetLib.h"
#import "HPClientEngine.h"
#import "SecureManager.h"
#import "Errors.h"
#import "ZipUtil.h"
#import "DefineUtil.h"
#import "ZZTongUIEngine.h"
#import "ZZTongClientEngine.h"
//#import "CSNValidator.h"
#import "RFCSNValidatorController.h"
#import "UIDevice+UIDevice_IpMacModel.h"
#import "HPUDIDTools.h"
#import "ZZTongDefine.h"
//#import "HPFunctionData.h"
//#import "DefineUtil.h"

//0-send 1-recieve
static NSString * HEADER_NAME_SESSIONID = @"X-HPSessionID";//0 1
static NSString * HEADER_NAME_Version = @"X-HPVersion";//0
static NSString * HEADER_NAME_SIGNATURE = @"X-HPSignature";//0 1
static NSString * HEADER_NAME_TerminalUserId = @"X-HPTerminalUserID";//0 1
static NSString * HEADER_NAME_KEYEXCHANGE = @"X-HPKeyExchange";//0 1

static NSString * HEADER_NAME_CONTENTTYPE = @"X-HPContentType";//1
static NSString * HEADER_NAME_DATALENGTH = @"X-HPDataLength";//1
static NSString * HEADER_NAME_COMPRESSED = @"X-HPCompressed";//1
static NSString * HEADER_NAME_CACHE = @"X-HPCache";//1

static NSString * HEADER_NAME_HOST = @"X-Online-Host";//0

static NSString * SECURE = @"secure";
static NSNetLib * instance = nil;


@implementation NSNetData
@synthesize url;
@synthesize action;
@synthesize params;
@synthesize header;
@synthesize responseDelegate;
@synthesize onlineHostHeader;
@synthesize isDoRSA;
@synthesize isShowAlert;
@synthesize isUploadImg;

-(id)initLoad:(NSString*)addrname withparams:(NSDictionary *)paramsi withheaders:(NSDictionary *)headersi withAction:(NSString*)actionnamei withDelegate:(id<NetCallBackDelegate>)delegate withAlert:(BOOL)flag witUploadImg:(BOOL)uploadImg{
    self = [super init];
    if (self) {
		self.url = [HPCLIENTENG getUrlByName:addrname];
		self.params = paramsi;
		self.action = actionnamei;
        self.header = headersi;
		self.responseDelegate = delegate;
		//self.nettype = NetLoadData;
        self.isDoRSA = NO;
        self.isShowAlert = flag;
        self.isUploadImg = uploadImg;
    }
    return self;
}

+(id)netDataWithData:(NSNetData*)netData{
    NSNetData* data = [[NSNetData alloc] initLoad:netData.url withparams:netData.params withheaders:netData.header withAction:netData.action withDelegate:netData.responseDelegate withAlert:netData.isShowAlert witUploadImg:netData.isUploadImg];
    data.isDoRSA = netData.isDoRSA;
    return [data autorelease];
}

-(void)dealloc{
    self.url = nil;
    self.action = nil;
    self.params = nil;
    self.header = nil;
    self.responseDelegate = nil;
    self.onlineHostHeader = nil;
    
    [super dealloc];
}


-(NSMutableDictionary*)buildRequestHeaders:(NSDictionary *)inputDic reqStr:(NSString*)req{
	NSMutableDictionary* resultDic = [NSMutableDictionary dictionaryWithDictionary:inputDic];
	
	HPClientEngine* ce = HPCLIENTENG;
    
	NSString* datas = [ce getHeadVersion];
	[resultDic setObject:datas forKey:HEADER_NAME_Version];
	
    NSString* terminalId = [ce GetRMSString:HEADER_NAME_TerminalUserId];
    
    if (terminalId && [terminalId length] > 0) {
        NSRange range = [terminalId rangeOfString:@","];
        if (range.length != 0) {
            terminalId = [terminalId substringToIndex:range.location];
        }
        [resultDic setObject:terminalId forKey:HEADER_NAME_TerminalUserId];
    }
	
    if (![NSString isNilOrEmpty:ce.securmanager.sessionId]) {
        [resultDic setObject:ce.securmanager.sessionId forKey:HEADER_NAME_SESSIONID];
    }
	if (self.onlineHostHeader) {
		[resultDic setObject:self.onlineHostHeader forKey:HEADER_NAME_HOST];
	}
	
    NSString* appUrl = [ce getUrlByName:APP_URL_NAME];
	if (![self.url isEqualToString:appUrl]){
        return resultDic;
    }
    [self fengkong:resultDic];
    
    NSRange needMac=[self.action rangeOfString:SECURE];
    if (needMac.location != NSNotFound) {
        NSString* mac = [ce.securmanager MAC:req withKey:nil];
        [resultDic setObject:mac forKey:HEADER_NAME_SIGNATURE];
    }
    
    NSString * seed=[ce.securmanager NeedCreatSecureKey];
    if (!seed) {
        return resultDic;
    }
    
    self.isDoRSA = YES;
    NSString* rsaStr = [ce.securmanager RSA:seed];
    if (rsaStr && [rsaStr length] > 0) {
        rsaStr = [@"testrsaid|" stringByAppendingString:rsaStr];
        [resultDic setObject:rsaStr forKey:HEADER_NAME_KEYEXCHANGE];
    }

	return resultDic;
}

// mcm new
-(void)fengkong:(NSMutableDictionary *)headerDic
{
    RFCSNValidatorController* validator = [RFCSNValidatorController sharedInstance];
    if (validator.location.length)
        [headerDic setObject:validator.location forKey:@"X-LOCATION"];
    if (validator.address.length)
        [headerDic setObject:validator.address forKey:@"X-ADDRESS"];
    
    if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)])
    {
        NSString * vendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        [headerDic setObject:vendor forKey:@"X-IMEI"];
    }
    else
    {
        NSString * mac = [[UIDevice currentDevice] getMacAddress];
        [headerDic setObject:mac forKey:@"X-MAC"];
    }

    NSString *udid = [HPUDIDTools UDID];
    if (udid)
        [headerDic setObject:udid forKey:@"X-HPTUDID"];
}

// mcm
//- (void)fengkong:(NSMutableDictionary *)headerDic
//{
//    NSString * location = [CSNValidator getLocation];
//    if (location)
//    {
//        [headerDic setObject:location forKey:@"X-LOCATION"];
//    }
//    NSString * address = [CSNValidator getAddress];
//    if (address)
//    {
//        [headerDic setObject:address forKey:@"X-ADDRESS"];
//    }
//    if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)])
//    {
//        NSString * vendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//        [headerDic setObject:vendor forKey:@"X-IMEI"];
//    }
//    else
//    {
//        NSString * mac = [[UIDevice currentDevice] getMacAddress];
//        [headerDic setObject:mac forKey:@"X-MAC"];
//    }
//    NSString *udid = [HPUDIDTools UDID];
//    if (udid) {
//        [headerDic setObject:udid forKey:@"X-HPTUDID"];
//    }
//}

-(NSDictionary *)treatResponseHeader:(HttpConnect *)connection{
	NSMutableDictionary* result = [NSMutableDictionary dictionaryWithCapacity:3];
	HPClientEngine* ce = HPCLIENTENG;
    NSDictionary* headers = connection.receiveHeaders;
    if (!headers) {
        return nil;
    }
    NSString* keyChange = nil;
    NSString* value = nil;
    NSString* sessionId = nil;
    
    for (NSString* key in headers) {
        value = [headers objectForKey:key];
        if([[key lowercaseString] isEqualToString:[HEADER_NAME_COMPRESSED lowercaseString]]){
            if (value && [[value lowercaseString] compare:@"true"] == NSOrderedSame) {
                NSData* tempData = [ZipUtil DepressData:connection.receiveData];
                connection.receiveData = [NSMutableData dataWithData:tempData];
            }
        }else if([[key lowercaseString] isEqualToString:[HEADER_NAME_DATALENGTH lowercaseString]]){
            if (!value) {
                continue;
            }
            connection.receiveData = [NSMutableData dataWithBytes:[connection.receiveData bytes] length:[value intValue]];
        }else if([[key lowercaseString] compare:[HEADER_NAME_CONTENTTYPE lowercaseString]]==NSOrderedSame){
            /*if ([[value lowercaseString] compare:@"data/binary"] == NSOrderedSame) {
             self.httpdata.datatype=HttpTypeDataCOMPILED;
             }else if([[cmp lowercaseString] compare:@"file"]==NSOrderedSame){
             self.httpdata.datatype=HttpTypeDataFILE;
             }else {
             self.httpdata.datatype=HttpTypeDataUNCOMPILED;
             }
             */
        }else if ([[key lowercaseString] isEqualToString:[HEADER_NAME_CACHE lowercaseString]]) {
            /*if ([value isEqualToString:@"persistent"]) {
             self.httpdata.savetype=FILE_URL;
             }else {
             self.httpdata.savetype=CACHE_URL;
             }*/
        }else if([[key lowercaseString] isEqualToString:[HEADER_NAME_TerminalUserId lowercaseString]]){
            [ce SaveRMSData:HEADER_NAME_TerminalUserId withStr:value];
        }else if([[key lowercaseString] isEqualToString:[HEADER_NAME_SIGNATURE lowercaseString]]){
            //self.signaturestr=[headers objectForKey:key];
            NSRange needMac=[self.action rangeOfString:SECURE];
            if (needMac.location == NSNotFound) {
                continue;
            }
            
            NSUInteger dataLen = [connection.receiveData length];
            NSString * crc = [ce.securmanager CalcCRC:[connection.receiveData bytes] withlen:dataLen];
            if (!value || ![[crc lowercaseString] isEqualToString:[value lowercaseString]]) {
                connection.errorCode = 13;
            }
        }else if([[key lowercaseString] isEqualToString:[HEADER_NAME_KEYEXCHANGE lowercaseString]]){
            keyChange = value;
        }else {
            if ([[key lowercaseString] isEqualToString:[HEADER_NAME_SESSIONID lowercaseString]]) {
                sessionId = value;
                ce.securmanager.sessionId = value;
            }
            
            //NSString * cmp=[headers objectForKey:key];
            if (value) {
                [result setObject:value forKey:key];
            }
        }
    }
    
    HPData* resultHPData = CreateHPDataFromString(connection.receiveData.bytes, "data", (int)[connection.receiveData length]);
    NSMutableDictionary* resultDic = [[[NSMutableDictionary alloc] init]autorelease];
    [self hpData2Dic:resultHPData result:resultDic];
    releaseHPData(resultHPData);
    
    NSString* errMsg = [resultDic objectForKey:NET_ERROR_MESSAGE];
    
    if ([NSString isNilOrEmpty:sessionId] && [self.action isEqualToString:NET_ACTION_ZZT_LOGIN] && (connection.errorCode == 0)) {
        
        if ([NSString isNilOrEmpty:errMsg]) {
            connection.errorCode = 3;
        }
    }
    if (self.isDoRSA) {
        [ce.securmanager ReSetSecure:keyChange];
        if (!keyChange && (connection.errorCode == 0) && [NSString isNilOrEmpty:errMsg]) {
            connection.errorCode = 2;
        }
    }
	
	return result;
}

-(NSString*)getErrorMsg:(int)errCode{
    if (errCode == 0) {
        return nil;
    }
    NSString* msg = nil;
    switch (errCode) {
        case 6:
            msg = [Errors getErrorMsg:ERR_MSG_TIMEOUT];
            break;
        case 8:
            msg = [Errors getErrorMsg:ERR_MSG_NETWORK];
            break;
        case 13:
            msg = [Errors getErrorMsg:ERR_MSG_SECURE];
            break;
        case 1:
            msg = [Errors getErrorMsg:ERR_MSG_NETWORK];
            break;
        case 2:
            msg = [Errors getErrorMsg:ERR_MSG_DATA];
            break;
        case 3:
            msg = [Errors getErrorMsg:ERR_MSG_LOGIN];
            break;
        default:
            msg = [Errors getErrorMsg:ERR_MSG_HTTP];
            break;
    }
    return msg;
}
-(NSDictionary *)getReturnDicData:(BOOL)isOver connection:(HttpConnect*)connection
{
    NSMutableDictionary *result = [[[NSMutableDictionary alloc]initWithCapacity:10]autorelease];


    int errorCode = connection.errorCode;
    NSNumber * errorCodeInt = [NSNumber numberWithInt:errorCode];
    [result setObject:errorCodeInt forKey:KEY_RESPCODE];
    
    NSString* errMsg = [self getErrorMsg:errorCode];
    if (errMsg) {
        
        [result setObject:errMsg forKey:KEY_RESPERRORMSG];
    }
    
	if (errorCode != 0) {
        return result;
    }
    
    if (connection.receiveHeaders) {
        [result setObject:connection.receiveHeaders forKey:KEY_RESPHEARDERS];
    }
    
    if (connection.receiveData) {
        [result setObject:connection.receiveData forKey:KEY_RESPDATA];
    }
    
	return result;
}

-(void)hpData2Dic:(const HPData *)srcData result:(NSMutableDictionary*)dic{
    if (!CHECKTABLE(srcData)) {
        return;
    }

    const HPNode * tempnode = srcData->datat->fielddatas;
    while (tempnode) {
        if (tempnode->key && tempnode->value) {
            NSString * key = [NSString stringWithUTF8String:tempnode->key];
            NSObject * value = nil;
            if (tempnode->value->type==LUA_TSTRING && tempnode->value->datas) {
                value = [NSString stringWithUTF8String:tempnode->value->datas];
            }else if(tempnode->value->type == LUA_TNUMBER){
                value = [NSString stringWithFormat:@"%.0f",tempnode->value->datad];
            }else if(tempnode->value->type == LUA_TBOOLEAN){
                value = tempnode->value->datab == 1 ? @"true" : @"false";
            }else if(tempnode->value->type == LUA_TTABLE && tempnode->value->datat){
                if (TableGetLength(tempnode->value) > 0) {
                    value = [[[NSMutableArray alloc] init] autorelease];
                    [self hpData2Array:tempnode->value result:(NSMutableArray*)value];
                }else{
                    value = [[[NSMutableDictionary alloc] init] autorelease];
                    [self hpData2Dic:tempnode->value result:(NSMutableDictionary*)value];
                }
            }
            if (value) {
                [dic setObject:value forKey:key];
            }
        }
        tempnode=tempnode->next;
    }
}

-(void)hpData2Array:(const HPData*)srcData result:(NSMutableArray*)array{
	if (!CHECKTABLE(srcData)) {
        return;
    }
    
    int itemsize = TableGetLength(srcData);
	for (int i = 0; i < itemsize; i++) {
		const HPData* itemObj=TableGetArrayIn(srcData,i);
		if (CHECKTABLE(itemObj)) {
            NSObject* tempObj = nil;
            if (TableGetLength(itemObj) > 0) {
                tempObj = [[NSMutableArray alloc] init];
                [self hpData2Array:itemObj result:(NSMutableArray*)tempObj];
            }else{
                tempObj = [[NSMutableDictionary alloc] init];
                [self hpData2Dic:itemObj result:(NSMutableDictionary*)tempObj];
            }
			
			[array addObject:tempObj];
            [tempObj release];
		}else if (CHECKSTRING(itemObj)) {
			NSString* tempStr = [NSString stringWithUTF8String:itemObj->datas];
			[array addObject:tempStr];
		}else if (CHECKNUM(itemObj)) {
            NSString* tempStr = [NSString stringWithFormat:@"%.0f", itemObj->datad];
            [array addObject:tempStr];
		}else if(CHECKBOOL(itemObj)){
            NSString* tempStr = itemObj->datab == 1 ? @"true" : @"false";
            [array addObject:tempStr];
        }
	}
}

-(void)doFailedCallback:(NSString*)errCode errMsg:(NSString*)errMsg{
    if ([self.responseDelegate respondsToSelector:@selector(responseCallBackFailed:withErrCode:withErrMsg:)]) {
        [self.responseDelegate responseCallBackFailed:self.action withErrCode:errCode withErrMsg:errMsg];
    }
}

-(void)doSuccessCallback:(NSDictionary*)respData{
    if ([self.responseDelegate respondsToSelector:@selector(responseCallBackSuccess:withData:)]) {
        [self.responseDelegate responseCallBackSuccess:self.action withData:respData];
    }
}


- (BOOL)checkError:(int)errorCode{
    if (!self.responseDelegate) {
        return NO;
    }
    
    if (errorCode != 0) {
        NSString* errMsg = [self getErrorMsg:errorCode];
        [self doFailedCallback:[NSString stringWithFormat:@"%d", errorCode] errMsg:errMsg];
        if (self.isShowAlert) {
            [HPUIENG showAlert:errMsg];
        }
        
        return NO;
    }
    
    return YES;
}

-(void)netDoOver:(HttpConnect*)connection{
    ZZTongClientEngine* zztEngine = [ZZTongClientEngine sharedInstance];
    zztEngine.netEngine.lastReqData = nil;
    
    if (![self checkError:connection.errorCode]) {
        return;
    }
    
    if (!connection.receiveData) {
        [self doSuccessCallback:nil];
        return;
    }
    
    HPData* resultHPData = CreateHPDataFromString(connection.receiveData.bytes, "data", (int)[connection.receiveData length]);
    NSMutableDictionary* resultDic = [[[NSMutableDictionary alloc] init]autorelease];
    [self hpData2Dic:resultHPData result:resultDic];
    releaseHPData(resultHPData);
    
    NSString* errMsg = [resultDic objectForKey:NET_ERROR_MESSAGE];
    NSString* response = [resultDic objectForKey:NET_RESPONSE_CODE];
    if (response && [response isEqualToString:@"99"]) {//登录超时
#pragma clang diagnostic push // mcm new. use marco for xcode keep warning slince.
#pragma clang diagnostic ignored "-Wundeclared-selector"
        AlertCommandData* commandRedo = [[AlertCommandData alloc] initCommand:@"确定" function:@selector(gotoLogin)];
        AlertData* alertData = [[AlertData alloc] initAlertData:ZZT_CLIENG title:@"提示" message:errMsg commands:commandRedo, nil];
#pragma clang diagnostic pop // mcm new
        [HPUIENG showAlertWithData:alertData];
        
        [commandRedo release];
        [alertData release];
//        [resultDic release];
        return;
    }
    if (![NSString isNilOrEmpty:errMsg]) {
        [self doFailedCallback:response errMsg:errMsg];
        if (isShowAlert) {
            [HPUIENG showAlert:errMsg];
        }
        
//        [resultDic release];
        return;
    }
    [self doSuccessCallback:resultDic];
    
#ifdef DEBUG
for(NSString* key in resultDic.allKeys)
{
    LOG(@"http response KEY:%@   value:%@",key,[resultDic objectForKey:key]);
}
#endif
   
//    [resultDic release];
    
}

-(NSString*)treatSpeicalChar:(NSString*)input{
    input=[input stringByReplacingOccurrencesOfString:@"%" withString:@"%25"];
    input=[input stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    input=[input stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    input=[input stringByReplacingOccurrencesOfString:@"+" withString:@"%2b"];
    input=[input stringByReplacingOccurrencesOfString:@"\'" withString:@"%27"];
    input=[input stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
    input=[input stringByReplacingOccurrencesOfString:@"." withString:@"%2E"];
    input=[input stringByReplacingOccurrencesOfString:@"<" withString:@"%3c"];
    input=[input stringByReplacingOccurrencesOfString:@">" withString:@"%3e"];
    input=[input stringByReplacingOccurrencesOfString:@"#" withString:@"%23"];
    input=[input stringByReplacingOccurrencesOfString:@"{" withString:@"%7b"];
    input=[input stringByReplacingOccurrencesOfString:@"}" withString:@"%7d"];
    input=[input stringByReplacingOccurrencesOfString:@"\\" withString:@"%5c"];
    input=[input stringByReplacingOccurrencesOfString:@"^" withString:@"%5e"];
    input=[input stringByReplacingOccurrencesOfString:@"~" withString:@"%73"];
    input=[input stringByReplacingOccurrencesOfString:@"[" withString:@"%5b"];
    input=[input stringByReplacingOccurrencesOfString:@"]" withString:@"%5d"];
    return input;
}

-(void)startConnect{
    if (HPCLIENTENG.netStatus == 0) {
        [self checkError:1];
        return;
    }
    
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    
    NSString* req = @"";
    for (id key in self.params){
        NSObject* value = [self.params objectForKey:key];
        if ([value isKindOfClass:[NSString class]]) {
            value = [self treatSpeicalChar:(NSString*)value];
            req = [NSString stringWithFormat:@"%@%@=%@&", req, key, value];
        }
        
    }
    if (req && [req length] > 0 && [req hasSuffix:@"&"]) {
        req = [req substringToIndex:[req length] - 1];
    }
    
    NSDictionary* headerDic = [self buildRequestHeaders:self.header reqStr:req];
    
    HttpConnect* connect = [[HttpConnect alloc] init];
    
    //如果不以.do结尾，补上.do
    NSString* tempAction = self.action;
    if (![self.action hasSuffix:@".do"]) {
        tempAction = [NSString stringWithFormat:@"%@.do", self.action];
    }
    
    if (self.isUploadImg) {
        [connect connectImg:[NSString stringWithFormat:@"%@%@", self.url, tempAction] req:self.params headers:headerDic];
    }else{
        [connect connect:[NSString stringWithFormat:@"%@%@", self.url, tempAction] req:req headers:headerDic];
    }
    [self treatResponseHeader:connect];
    
    [self netDoOver:connect];
    
    [connect release];
    
    [pool release];
}

-(BOOL)isConcurrent{
    return YES; // 异步
}

-(void)main{
    [self startConnect];
}

@end


@implementation NSNetLib
@synthesize requsetQueue;

//+(void)NetLoad:(NSString *)addrname withAction:(NSString *)action withParams:(NSDictionary *)params withHeaders:(NSDictionary *) headers withFunc:(id<HPResponseCallBackDelegate>)func withAlert:(BOOL)isShowAlert
//{
//    
//    NSNetData* netData = [[NSNetData alloc] initLoad:addrname withparams:params withheaders:headers withAction:action withfunc:func withAlert:isShowAlert];
//    
//    [[NSNetLib sharedInstance] startRequest:netData];
//    
//    [netData release];
//}


+(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate{
    [NSNetLib sendRequest:action withParams:params withDelegate:delegate withAddress:APP_URL_NAME withAlertFlag:YES];
}

+(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAddress:(NSString*)addrName{
    [NSNetLib sendRequest:action withParams:params withDelegate:delegate withAddress:addrName withAlertFlag:YES];
}

+(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAlertFlag:(BOOL)isShowAlert{
    [NSNetLib sendRequest:action withParams:params withDelegate:delegate withAddress:APP_URL_NAME withAlertFlag:isShowAlert];
}

+(void)sendRequest:(NSString*)action withParams:(NSDictionary*)params withDelegate:(id<NetCallBackDelegate>)delegate withAddress:(NSString*)addrName withAlertFlag:(BOOL)isShowAlert{
    
    NSNetData* netData = [[NSNetData alloc] initLoad:addrName withparams:params withheaders:nil withAction:action withDelegate:delegate withAlert:isShowAlert witUploadImg:NO];
    [[NSNetLib sharedInstance] startRequest:netData];
    [netData release];
    
}

-(void)startRequest:(NSNetData*)requsetData{
    if (!requsetData) {
        return;
    }
    @synchronized(self.requsetQueue){
        [self.requsetQueue addOperation:requsetData];
    }
}

-(void)initCustom{
    NSOperationQueue* queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:1];
    self.requsetQueue = queue;
    [queue release];
}

+(NSNetLib*)sharedInstance{
    @synchronized(instance){
        if (!instance) {
            instance = [[NSNetLib alloc] init];
            [instance initCustom];
        }
    }
    return instance;
}

+(void)releaseInstance{
    if (instance) {
        [instance release];
        instance = nil;
    }
}

-(void)dealloc{
    self.requsetQueue = nil;
    [super dealloc];
}
@end